package com.objetdirect.gwt.umldrawer.server.services;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.objetdirect.gwt.umldrawer.server.dao.DeveloperDao;
import com.objetdirect.gwt.umldrawer.shared.dto.DeveloperDto;
import com.objetdirect.gwt.umldrawer.shared.services.DeveloperService;

@Service("developerService")
public class DeveloperServiceImpl implements DeveloperService {

	@Autowired
	private DeveloperDao developerDao;

	@PostConstruct
	public void init() throws Exception {
	}

	@PreDestroy
	public void destroy() {
	}

	public DeveloperDto findDeveloper(String developerId) {
		return developerDao.findById(developerId);
	}

	@Override
	public boolean signIn(String developerEmail, String password) {
		DeveloperDto developerDto = developerDao.findById(developerEmail);
		boolean isFound = false;
		if(developerDto != null)
			if(developerDto.getPassword().equals(password)){
				isFound = true;
			}
		
		return isFound;
	}
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void saveDeveloper(String developerEmail, String firstName,
			String lastName, String password, int onlineStatus)
			throws Exception {

		DeveloperDto DeveloperDto = developerDao.findById(developerEmail);

		if (DeveloperDto == null) {
			DeveloperDto = new DeveloperDto(developerEmail, firstName,
					lastName, password, onlineStatus);
			developerDao.persist(DeveloperDto);
		}

	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void updateDeveloper(String developerEmail, String firstName,
			String lastName, String password, int onlineStatus)
			throws Exception {

		DeveloperDto DeveloperDto = developerDao.findById(developerEmail);

		if (DeveloperDto != null) {
			DeveloperDto.setFirstName(firstName);
			DeveloperDto.setLastName(lastName);
			DeveloperDto.setPassword(password);
			DeveloperDto.setOnlineStatus(onlineStatus);
		}

	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void deleteDeveloper(String developerEmail) throws Exception {

		DeveloperDto DeveloperDto = developerDao.findById(developerEmail);

		if (DeveloperDto != null)
			developerDao.remove(DeveloperDto);

	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void saveOrUpdateDeveloper(String developerEmail, String firstName,
			String lastName, String password, int onlineStatus)
			throws Exception {

		DeveloperDto DeveloperDto = new DeveloperDto(developerEmail, firstName,
				lastName, password, onlineStatus);

		developerDao.merge(DeveloperDto);

	}

}
